//
//  PrivacyAgreeView.m
//  阿帕
//
//  Created by mac on 2020/3/18.
//  Copyright © 2020 GCY. All rights reserved.
//

#import "PrivacyAgreeView.h"

@interface PrivacyAgreeView ()

@end

@implementation PrivacyAgreeView

+ (PrivacyAgreeView *)shared {
    
    static dispatch_once_t predicate;
    
    static PrivacyAgreeView *objc;
    
    dispatch_once(&predicate, ^{
        
        objc = [[PrivacyAgreeView alloc] init];
        
    });
    
    return objc;
}

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setUpUI];
    }
    
    return self;
}

- (void)setUpUI {
    
    self.frame = CGRectMake(0, 0, ScreenW, ScreenH);
        
    //黑色遮挡层
    UIView *barrierView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH)];
    barrierView.backgroundColor = RGBA(0, 0, 0, 0.5);
    [self addSubview:barrierView];
    
    NSString *messageStr = @"欢迎您使用我们的产品和服务！我们非常重视用户的隐私和个人信息保护。请您认真阅读并同意服务协议和隐私政策，以了解我们的服务内容和我们在使用您相关信息时的处理规则。我们将严格按照服务协议和隐私政策为您提供服务，保护您的个人信息。";
    
    CGSize size = [DCSpeedy dc_calculateTextSizeWithText:messageStr WithTextFont:14 WithMaxW:ScreenW-140];
    
    //确认信息底层白色View
    UIView *confirView = [[UIView alloc] initWithFrame:CGRectMake(50, 0, ScreenW-100, size.height + 220)];
    confirView.cy_centerX = self.cy_centerX;
    confirView.cy_centerY = self.cy_centerY-50;
    confirView.backgroundColor = RGB(255, 255, 255);
    confirView.userInteractionEnabled = YES;
    [DCSpeedy dc_chageControlCircularWith:confirView AndSetCornerRadius:10 SetBorderWidth:0 SetBorderColor:nil canMasksToBounds:YES];
    [self addSubview:confirView];
    

    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, confirView.width, 60)];
    titleLabel.text = @"用户须知";
    titleLabel.textColor = Dark_TextColor;
    titleLabel.font = font(18);
    titleLabel.textAlignment= NSTextAlignmentCenter;
    [confirView addSubview:titleLabel];
    
    UILabel *msgLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, titleLabel.cy_bottom, confirView.width-40, size.height)];
    msgLabel.text = messageStr;
    msgLabel.textColor = Dark_TextColor;
    msgLabel.font = font(14);
    msgLabel.numberOfLines = 0;
    msgLabel.adjustsFontSizeToFitWidth = YES;
    msgLabel.textAlignment = NSTextAlignmentCenter;
    [confirView addSubview:msgLabel];
    
    NSArray *titles = @[@"同意", @"不同意并退出APP"];
    
    for (int i=0; i<2; i++) {
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.tag = i + 300;
        [button setTitle:titles[i] forState:UIControlStateNormal];
        button.titleLabel.font = font(16);
        [button setTitleColor:i==0?[UIColor whiteColor]:Theme_ColorValue forState:UIControlStateNormal];
        [button setBackgroundColor:i==0?Theme_ColorValue:[UIColor whiteColor]];
        button.frame = CGRectMake(20, msgLabel.cy_bottom + 55 + 45 * i, confirView.width-40, 35);
        cut(button, BTN_BORDER_RADIUS);
        if (i==1) {
            cutBorder(button, 1, @"#2C7CF6")
        }
        [button addTarget:self action:@selector(btnEventClick:) forControlEvents:UIControlEventTouchUpInside];
        [confirView addSubview:button];
    }
}

- (void)btnEventClick:(UIButton *)button {
    
    if (self.block) {
        if (button.tag == 300) {
            self.block(YES);
            [self hidden];
        } else {
            self.block(NO);
        }
    }
}

- (void)show {
    
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    
    [UIView animateWithDuration:0.5 animations:^{
        self.alpha = 1.0;
    }];
}

- (void)hidden {
    
    [UIView animateWithDuration:0.5 animations:^{
        self.alpha = 0.0;
    }];
}

@end
